﻿//by Navy Zhang 710605420@qq.com
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Common;

public class AndroidMemoryUsage :Singleton<AndroidMemoryUsage> 
{

    private AndroidJavaClass unityJc = null;
    private AndroidJavaObject unityContext = null;
    private AndroidJavaClass memoryUseageJc = null;

    public void Initialized()
    {
        unityJc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityContext = unityJc.GetStatic<AndroidJavaObject>("currentActivity");
        memoryUseageJc = new AndroidJavaClass("com.tencent.memoryuseLibrary.MemoryHelper");
        memoryUseageJc.CallStatic("Init", unityContext);
    }

    public float GetMemoryXmsMax()
    {
        if(memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetMemoryXmsMax");
        }
        else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }
    public float GetMemoryXmsFree()
    {
        if (memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetMemoryXmsFree");
        }
        else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }
    public float GetMemoryXmsTotalUsage()
    {
        if (memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetMemoryXmsTotalUsage");
        }
        else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }

    public float GetNativeMaxSize()
    {
        if (memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetNativeMaxSize");
        }
        else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }

    public float GetNativeFreeSize()
    {
        if (memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetNativeFreeSize");
        }
        else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }

    public float GetNativeAllocatedSize()
    {
        if (memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetNativeAllocatedSize");
        }else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }

    public float GetProcessPss()
    {
        //java 5s 计算一次
        if (memoryUseageJc != null)
        {
            return memoryUseageJc.CallStatic<float>("GetProcessPss");
        }
        else
        {
            Debug.LogError("sdk initial failed");
        }
        return 0f;
    }
}
